//
//  MyShortVideoSettingVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/9/26.
//

#import "MyShortVideoSettingVC.h"
#import "SYCollectionViewRoundFlowLayout.h"
#import "QZShortPlayVCCollectionViewCell.h"
#import <PangrowthDJX/DJXSDK.h>
#import <LCDSDK/LCDSDK.h>
@interface MyShortVideoSettingVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong,nonatomic)UICollectionView * historyCollection;
@property (strong,nonatomic)NSMutableArray<DJXPlayletInfoModel *> * videoArray;
@property (assign,nonatomic)NSInteger requestPage;
@property (strong,nonatomic)PublicView * nullView;
@property (assign,nonatomic)NSInteger item_w;
@end

@implementation MyShortVideoSettingVC

- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(self.shortType == 0){
        self.navigationItem.title = @"短剧浏览";
    }else{
        self.navigationItem.title = @"短剧收藏";
    }
    KWeakSelf
    self.item_w = (KScreen_W-50)/3;
    self.historyCollection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.requestPage = weakSelf.requestPage+1;
        [weakSelf djRequest];
    }];
    self.historyCollection.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //下拉刷新要做的操作.
        [weakSelf.historyCollection.mj_footer resetNoMoreData];
        [weakSelf.videoArray removeAllObjects];
        [weakSelf.historyCollection reloadData];
        [weakSelf djRequest];
    }];
    
    _videoArray = [NSMutableArray new];
    _requestPage = 1;
    
    [SVProgressHUDTool showToastMessage:nil];
    [self djRequest];
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-KNav_H-200)/2, KScreen_W, 200)];
        _nullView.publicLabel.text = @"暂无历史记录";
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    NSLog(@"");
}
- (void)djRequest{
    KWeakSelf
    if(self.shortType == 0){
        [[DJXPlayletManager shareInstance] requestPlayletHistoryListWithPage:self.requestPage num:30 success:^(NSArray<DJXPlayletInfoModel *> * _Nonnull playletList) {
            [weakSelf.videoArray addObjectsFromArray:playletList];
            [weakSelf.historyCollection reloadData];
            [weakSelf requestEnd:playletList];
        } failure:^(NSError * _Nonnull error) {
            if(weakSelf.requestPage > 1){
                weakSelf.requestPage--;
            }
            [YBMsgPop showPop:@"加载失败"];
        }];
    }else if(self.shortType == 1){
        [[DJXPlayletManager shareInstance] requestCollectionList:self.requestPage pageSize:30 success:^(NSArray<DJXPlayletInfoModel *> * _Nonnull playletList, BOOL hasMore) {
            [weakSelf.videoArray addObjectsFromArray:playletList];
            [weakSelf.historyCollection reloadData];
            [weakSelf requestEnd:playletList];
        } failure:^(NSError * _Nonnull error) {
            if(weakSelf.requestPage > 1){
                weakSelf.requestPage--;
            }
            [YBMsgPop showPop:@"加载失败"];
        }];
    }
}
- (void)requestEnd:(NSArray<DJXPlayletInfoModel *> *)list{
    if([self.historyCollection.mj_header isRefreshing]){
        [self.historyCollection.mj_header endRefreshing];
    }else if ([self.historyCollection.mj_footer isRefreshing]){
        if([list count] == 0){
            [self.historyCollection.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.historyCollection.mj_footer endRefreshing];
        }
    }else{
        if(self.videoArray.count == 0){
            self.nullView.hidden = NO;
            self.historyCollection.mj_footer.hidden = YES;
        }else{
            self.nullView.hidden = YES;
            self.historyCollection.mj_footer.hidden = NO;
        }
    }
}
- (NSMutableArray *)videoArray{
    if(_videoArray == nil){
        _videoArray = [NSMutableArray new];
    }
    return _videoArray;
}
- (UICollectionView *)historyCollection{
    if(_historyCollection == nil){
        
        SYCollectionViewRoundFlowLayout *layout = [[SYCollectionViewRoundFlowLayout alloc]init];
        layout.sectionHeadersPinToVisibleBounds = YES;
        layout.collectionCellAlignmentType = SYCollectionViewFlowLayoutAlignmentTypeByLeft;
        _historyCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KNav_H-KSafeAreaBottomHeight) collectionViewLayout:layout];
        [_historyCollection registerNib:[UINib nibWithNibName:@"QZShortPlayVCCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"QZShortPlayVCCollectionViewCell"];
        _historyCollection.backgroundColor = viewback_black;
        
        _historyCollection.alwaysBounceVertical = YES;
        _historyCollection.delegate = self;
        _historyCollection.dataSource = self;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_historyCollection];
    }
    return _historyCollection;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.videoArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QZShortPlayVCCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZShortPlayVCCollectionViewCell" forIndexPath:indexPath];
    [cell findsShoeCell:self.videoArray[indexPath.row]];
    return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
 layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, KSafeAreaBottomHeight, 15);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.item_w, 200);
}
- (void)showTextViewSubViewToViewTo1:(UIView * )subView{
    
    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DJXPlayletConfig *config = [DJXPlayletConfig new];
    if([[QZUserInfoModel share].mid intValue] == 2){
        config.freeEpisodesCount = 10000;
    }else if([[QZUserInfoModel share].iscreator intValue] == 1){
        config.freeEpisodesCount = 20;
    }else{
        config.freeEpisodesCount = 5;
    }
    config.unlockEpisodesCountUsingAD = 10;
    config.groupId = self.videoArray[indexPath.row].channel_id;
    config.playStartTime = self.videoArray[indexPath.row].unlock_index;
    config.skitId = self.videoArray[indexPath.row].shortplay_id;//model.skit_id ?: model.shortplay_id;
    config.episode = 0;
    config.playletUnlockADMode = DJXPlayletUnlockADMode_Common;
    config.fromTopMargin = 0;
    DJXDrawVideoViewController * playVC = [[DJXPlayletManager shareInstance] playletViewControllerWithParams:config];
    playVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:playVC animated:YES completion:nil];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
